/*     */ package blockbuster.components.appearance;
/*     */ 
/*     */ import blockbuster.BedrockSchemeJsonAdapter;
/*     */ import blockbuster.emitter.BedrockParticle;
/*     */ import blockbuster.math.Constant;
/*     */ import blockbuster.math.IValue;
/*     */ import blockbuster.math.MathUtils;
/*     */ import blockbuster.math.molang.MolangException;
/*     */ import blockbuster.math.molang.MolangParser;
/*     */ import blockbuster.math.molang.expressions.MolangExpression;
/*     */ import blockbuster.math.molang.expressions.MolangValue;
/*     */ import blockbuster.utils.Interpolations;
/*     */ import com.google.gson.JsonArray;
/*     */ import com.google.gson.JsonElement;
/*     */ import com.google.gson.JsonObject;
/*     */ import com.google.gson.JsonPrimitive;
/*     */ import java.math.RoundingMode;
/*     */ import java.text.DecimalFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.lang3.StringUtils;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class Tint
/*     */ {
/*     */   public static Solid parseColor(JsonElement element, MolangParser parser) throws MolangException {
/*  32 */     MolangExpression r = MolangParser.ONE;
/*  33 */     MolangExpression g = MolangParser.ONE;
/*  34 */     MolangExpression b = MolangParser.ONE;
/*  35 */     MolangExpression a = MolangParser.ONE;
/*     */     
/*  37 */     if (element.isJsonPrimitive()) {
/*     */       
/*  39 */       String hex = element.getAsString();
/*     */       
/*  41 */       if (hex.startsWith("#") && (hex.length() == 7 || hex.length() == 9))
/*     */       {
/*  43 */         boolean hasAlpha = (hex.length() == 9);
/*     */ 
/*     */ 
/*     */ 
/*     */         
/*     */         try {
/*  49 */           int color = Integer.parseInt(hex.substring(hasAlpha ? 3 : 1), 16);
/*  50 */           float hr = (color >> 16 & 0xFF) / 255.0F;
/*  51 */           float hg = (color >> 8 & 0xFF) / 255.0F;
/*  52 */           float hb = (color & 0xFF) / 255.0F;
/*  53 */           float ha = hasAlpha ? (Integer.parseInt(hex.substring(1, 3), 16) / 255.0F) : 1.0F;
/*     */           
/*  55 */           MolangValue molangValue1 = new MolangValue(parser, (IValue)new Constant(hr));
/*  56 */           MolangValue molangValue2 = new MolangValue(parser, (IValue)new Constant(hg));
/*  57 */           MolangValue molangValue3 = new MolangValue(parser, (IValue)new Constant(hb));
/*  58 */           MolangValue molangValue4 = new MolangValue(parser, (IValue)new Constant(ha));
/*     */         }
/*  60 */         catch (Exception exception) {}
/*     */       }
/*     */     
/*     */     }
/*  64 */     else if (element.isJsonArray()) {
/*     */       
/*  66 */       JsonArray array = element.getAsJsonArray();
/*     */       
/*  68 */       if (array.size() == 3 || array.size() == 4) {
/*     */         
/*  70 */         r = parser.parseJson(array.get(0));
/*  71 */         g = parser.parseJson(array.get(1));
/*  72 */         b = parser.parseJson(array.get(2));
/*     */         
/*  74 */         if (array.size() == 4)
/*     */         {
/*  76 */           a = parser.parseJson(array.get(3));
/*     */         }
/*     */       } 
/*     */     } 
/*     */     
/*  81 */     return new Solid(r, g, b, a);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static Tint parseGradient(JsonObject color, MolangParser parser) throws MolangException {
/*  89 */     JsonElement gradient = color.get("gradient");
/*     */     
/*  91 */     MolangExpression expression = MolangParser.ZERO;
/*  92 */     List<Gradient.ColorStop> colorStops = new ArrayList<>();
/*  93 */     boolean equal = true;
/*     */     
/*  95 */     if (gradient.isJsonObject()) {
/*     */       
/*  97 */       for (Map.Entry<String, JsonElement> entry : (Iterable<Map.Entry<String, JsonElement>>)gradient.getAsJsonObject().entrySet()) {
/*     */         
/*  99 */         Solid stopColor = parseColor(entry.getValue(), parser);
/*     */         
/* 101 */         colorStops.add(new Gradient.ColorStop(Float.parseFloat(entry.getKey()), stopColor));
/*     */       } 
/*     */       
/* 104 */       colorStops.sort((a, b) -> Float.compare(a.stop, b.stop));
/* 105 */       equal = false;
/*     */     }
/* 107 */     else if (gradient.isJsonArray()) {
/*     */       
/* 109 */       JsonArray colors = gradient.getAsJsonArray();
/*     */       
/* 111 */       int i = 0;
/*     */       
/* 113 */       for (JsonElement stop : colors) {
/*     */         
/* 115 */         colorStops.add(new Gradient.ColorStop(i / (colors.size() - 1), parseColor(stop, parser)));
/*     */         
/* 117 */         i++;
/*     */       } 
/*     */     } 
/*     */     
/* 121 */     float range = ((Gradient.ColorStop)colorStops.get(colorStops.size() - 1)).stop;
/* 122 */     DecimalFormat floatPrecision = new DecimalFormat("#.######");
/*     */     
/* 124 */     floatPrecision.setRoundingMode(RoundingMode.HALF_EVEN);
/*     */     
/* 126 */     for (Gradient.ColorStop stop : colorStops) {
/*     */       
/* 128 */       stop.stop /= range;
/* 129 */       stop.stop = Float.valueOf(floatPrecision.format(stop.stop)).floatValue();
/*     */     } 
/*     */     
/* 132 */     if (color.has("interpolant"))
/*     */     {
/* 134 */       expression = parser.parseJson(color.get("interpolant"));
/*     */     }
/*     */     
/* 137 */     return new Gradient(colorStops, range, expression, equal);
/*     */   }
/*     */ 
/*     */   
/*     */   public abstract void compute(BedrockParticle paramBedrockParticle);
/*     */ 
/*     */   
/*     */   public abstract JsonElement toJson();
/*     */ 
/*     */   
/*     */   public static class Solid
/*     */     extends Tint
/*     */   {
/*     */     public MolangExpression r;
/*     */     public MolangExpression g;
/*     */     public MolangExpression b;
/*     */     public MolangExpression a;
/*     */     
/*     */     public Solid(MolangExpression r, MolangExpression g, MolangExpression b, MolangExpression a) {
/* 156 */       this.r = r;
/* 157 */       this.g = g;
/* 158 */       this.b = b;
/* 159 */       this.a = a;
/*     */     }
/*     */ 
/*     */     
/*     */     public Solid() {
/* 164 */       this.r = MolangParser.ONE;
/* 165 */       this.g = MolangParser.ONE;
/* 166 */       this.b = MolangParser.ONE;
/* 167 */       this.a = MolangParser.ONE;
/*     */     }
/*     */ 
/*     */     
/*     */     public boolean isConstant() {
/* 172 */       return (MolangExpression.isExpressionConstant(this.r) && MolangExpression.isExpressionConstant(this.g) && 
/* 173 */         MolangExpression.isExpressionConstant(this.b) && MolangExpression.isExpressionConstant(this.a));
/*     */     }
/*     */ 
/*     */ 
/*     */     
/*     */     public void compute(BedrockParticle particle) {
/* 179 */       particle.r = (float)this.r.get();
/* 180 */       particle.g = (float)this.g.get();
/* 181 */       particle.b = (float)this.b.get();
/* 182 */       particle.a = (float)this.a.get();
/*     */     }
/*     */ 
/*     */ 
/*     */     
/*     */     public JsonElement toJson() {
/* 188 */       JsonArray array = new JsonArray();
/*     */       
/* 190 */       if (MolangExpression.isOne(this.r) && MolangExpression.isOne(this.g) && MolangExpression.isOne(this.b) && MolangExpression.isOne(this.a))
/*     */       {
/* 192 */         return (JsonElement)array;
/*     */       }
/*     */       
/* 195 */       array.add(this.r.toJson());
/* 196 */       array.add(this.g.toJson());
/* 197 */       array.add(this.b.toJson());
/* 198 */       array.add(this.a.toJson());
/*     */       
/* 200 */       return (JsonElement)array;
/*     */     }
/*     */ 
/*     */     
/*     */     public JsonElement toHexJson() {
/* 205 */       int r = (int)(this.r.get() * 255.0D) & 0xFF;
/* 206 */       int g = (int)(this.g.get() * 255.0D) & 0xFF;
/* 207 */       int b = (int)(this.b.get() * 255.0D) & 0xFF;
/* 208 */       int a = (int)(this.a.get() * 255.0D) & 0xFF;
/*     */       
/* 210 */       String hex = "#";
/*     */       
/* 212 */       if (a < 255)
/*     */       {
/* 214 */         hex = hex + StringUtils.leftPad(Integer.toHexString(a), 2, "0").toUpperCase();
/*     */       }
/*     */       
/* 217 */       hex = hex + StringUtils.leftPad(Integer.toHexString(r), 2, "0").toUpperCase();
/* 218 */       hex = hex + StringUtils.leftPad(Integer.toHexString(g), 2, "0").toUpperCase();
/* 219 */       hex = hex + StringUtils.leftPad(Integer.toHexString(b), 2, "0").toUpperCase();
/*     */       
/* 221 */       return (JsonElement)new JsonPrimitive(hex);
/*     */     }
/*     */ 
/*     */     
/*     */     public void lerp(BedrockParticle particle, float factor) {
/* 226 */       particle.r = Interpolations.lerp(particle.r, (float)this.r.get(), factor);
/* 227 */       particle.g = Interpolations.lerp(particle.g, (float)this.g.get(), factor);
/* 228 */       particle.b = Interpolations.lerp(particle.b, (float)this.b.get(), factor);
/* 229 */       particle.a = Interpolations.lerp(particle.a, (float)this.a.get(), factor);
/*     */     }
/*     */   }
/*     */ 
/*     */   
/*     */   public static class Gradient
/*     */     extends Tint
/*     */   {
/*     */     public List<ColorStop> stops;
/*     */     
/*     */     public MolangExpression interpolant;
/*     */     
/* 241 */     public float range = 1.0F;
/*     */     
/*     */     public boolean equal;
/*     */     
/*     */     public Gradient(List<ColorStop> stops, float range, MolangExpression interpolant, boolean equal) {
/* 246 */       this.stops = stops;
/* 247 */       this.range = range;
/* 248 */       this.interpolant = interpolant;
/* 249 */       this.equal = equal;
/*     */     }
/*     */ 
/*     */     
/*     */     public Gradient() {
/* 254 */       this.stops = new ArrayList<>();
/* 255 */       this.stops.add(new ColorStop(0.0F, new Tint.Solid((MolangExpression)new MolangValue(null, (IValue)new Constant(1.0D)), (MolangExpression)new MolangValue(null, (IValue)new Constant(1.0D)), (MolangExpression)new MolangValue(null, (IValue)new Constant(1.0D)), (MolangExpression)new MolangValue(null, (IValue)new Constant(1.0D)))));
/* 256 */       this.stops.add(new ColorStop(1.0F, new Tint.Solid((MolangExpression)new MolangValue(null, (IValue)new Constant(0.0D)), (MolangExpression)new MolangValue(null, (IValue)new Constant(0.0D)), (MolangExpression)new MolangValue(null, (IValue)new Constant(0.0D)), (MolangExpression)new MolangValue(null, (IValue)new Constant(1.0D)))));
/* 257 */       this.interpolant = MolangParser.ZERO;
/* 258 */       this.equal = false;
/*     */     }
/*     */ 
/*     */     
/*     */     public void sort() {
/* 263 */       this.stops.sort((a, b) -> Float.compare(a.stop, b.stop));
/*     */     }
/*     */ 
/*     */ 
/*     */     
/*     */     public void compute(BedrockParticle particle) {
/* 269 */       int length = this.stops.size();
/*     */       
/* 271 */       if (length == 0) {
/*     */         
/* 273 */         particle.r = particle.g = particle.b = particle.a = 1.0F;
/*     */         
/*     */         return;
/*     */       } 
/* 277 */       if (length == 1) {
/*     */         
/* 279 */         ((ColorStop)this.stops.get(0)).color.compute(particle);
/*     */         
/*     */         return;
/*     */       } 
/*     */       
/* 284 */       double factor = this.interpolant.get();
/*     */       
/* 286 */       factor = MathUtils.clamp(factor, 0.0D, 1.0D);
/*     */       
/* 288 */       ColorStop prev = this.stops.get(0);
/*     */       
/* 290 */       if (factor < prev.getStop(this.range)) {
/*     */         
/* 292 */         prev.color.compute(particle);
/*     */         
/*     */         return;
/*     */       } 
/*     */       
/* 297 */       for (int i = 1; i < length; i++) {
/*     */         
/* 299 */         ColorStop stop = this.stops.get(i);
/*     */         
/* 301 */         if (stop.getStop(this.range) > factor) {
/*     */           
/* 303 */           prev.color.compute(particle);
/* 304 */           stop.color.lerp(particle, (float)(factor - prev.getStop(this.range)) / (stop.getStop(this.range) - prev.getStop(this.range)));
/*     */           
/*     */           return;
/*     */         } 
/*     */         
/* 309 */         prev = stop;
/*     */       } 
/*     */       
/* 312 */       prev.color.compute(particle);
/*     */     }
/*     */ 
/*     */ 
/*     */     
/*     */     public JsonElement toJson() {
/* 318 */       JsonObject jsonObject1, object = new JsonObject();
/*     */ 
/*     */       
/* 321 */       if (this.equal) {
/*     */         
/* 323 */         JsonArray gradient = new JsonArray();
/*     */         
/* 325 */         for (ColorStop stop : this.stops)
/*     */         {
/* 327 */           gradient.add(stop.color.toHexJson());
/*     */         }
/*     */         
/* 330 */         JsonArray jsonArray1 = gradient;
/*     */       }
/*     */       else {
/*     */         
/* 334 */         JsonObject gradient = new JsonObject();
/*     */         
/* 336 */         for (ColorStop stop : this.stops)
/*     */         {
/* 338 */           gradient.add(String.valueOf(stop.getStop(this.range)), stop.color.toHexJson());
/*     */         }
/*     */         
/* 341 */         jsonObject1 = gradient;
/*     */       } 
/*     */       
/* 344 */       if (!BedrockSchemeJsonAdapter.isEmpty((JsonElement)jsonObject1))
/*     */       {
/* 346 */         object.add("gradient", (JsonElement)jsonObject1);
/*     */       }
/*     */       
/* 349 */       if (!MolangExpression.isZero(this.interpolant))
/*     */       {
/* 351 */         object.add("interpolant", this.interpolant.toJson());
/*     */       }
/*     */       
/* 354 */       return (JsonElement)object;
/*     */     }
/*     */ 
/*     */     
/*     */     public static class ColorStop
/*     */     {
/*     */       public float stop;
/*     */       public Tint.Solid color;
/*     */       
/*     */       public ColorStop(float stop, Tint.Solid color) {
/* 364 */         this.stop = stop;
/* 365 */         this.color = color;
/*     */       }
/*     */       
/*     */       public float getStop(float range)
/*     */       {
/* 370 */         return this.stop * range; } } } public static class ColorStop { public float getStop(float range) { return this.stop * range; }
/*     */ 
/*     */     
/*     */     public float stop;
/*     */     public Tint.Solid color;
/*     */     
/*     */     public ColorStop(float stop, Tint.Solid color) {
/*     */       this.stop = stop;
/*     */       this.color = color;
/*     */     } }
/*     */ 
/*     */ }


/* Location:              D:\fanbian\[D][龙之核心]DragonCore-2.6.0.3.jar!\blockbuster\components\appearance\Tint.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */